<?php

namespace app\api\controller\v1;

use app\admin\model\Userweixin;
use app\api\service\CommonService;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\User;
use EasyWeChat\Factory;
use fast\Random;
use think\Validate;

/**
 * 用户实名
 */
class Userverify extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $app = null;

    public function _initialize()
    {
        parent::_initialize();

        $config = [
            'app_id' => 'wx24f19287ae7f5d5e',
            'secret' => '4d76532cde1d11b0e03054083eddea4b',

            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
            ],
        ];
        $this->app = Factory::miniProgram($config);
    }


    public function verify(CommonService $commonService)
    {

        $username = $this->request->param('username');
        $idcard = $this->request->param('idcard');
        $id = $this->request->param('id');
        $user = $this->auth->getUser();

        $userVerifyModel = new \app\admin\model\Userverify();
        $userModel = new User();
        $ver = $userVerifyModel->where('idcard', $idcard)->where('status', 1)->find();
        if ($ver) {
            $this->error("此身份证号已被认证");
        }

        $uid = $this->auth->getUser()['id'];
        $userVerify = $userVerifyModel->where('uid', $uid)->find();

        if ($userVerify) {
            if ($userVerify['status'] != 1) {

                $insertData = array();
                $insertData['username'] = $username;
                $insertData['idcard'] = $idcard;
                $insertData['createtime'] = time();
                $insertData['uid'] = $user['id'];
                $userVerifyModel->where('id', $userVerify['id'])->update($insertData);

                $ret = $commonService->verify($idcard, $username);
                $obj = json_decode($ret, true);
                $status = $obj['status'];
                $msg = $obj['msg'];
                $dbData = array();
                if ($status == '01') {//通过
                    $dbData['status'] = 1;
                    $userModel->where('id', $user['id'])->update(['username' => $username]);
                } else {
                    $dbData['status'] = -1;
                }
                $dbData['mark'] = $msg;
                $userVerifyModel->where('id', $id)->update($dbData);
                $this->success("修改成功");

            }
        } else {
            $insertData = array();
            $insertData['username'] = $username;
            $insertData['idcard'] = $idcard;
            $insertData['createtime'] = time();
            $insertData['uid'] = $user['id'];
            $id = $userVerifyModel->insertGetId($insertData);
            if ($id) {

                $ret = $commonService->verify($idcard, $username);

                $obj = json_decode($ret, true);
                $status = $obj['status'];
                $msg = $obj['msg'];
                $dbData = array();

                error_log("======status =====" . $status);
                if ($status == '01') {//通过

                    $dbData['status'] = 1;
                    $userModel->where('id', $user['id'])->update(['username' => $username]);
                } else {
                    $dbData['status'] = -1;
                }
                $dbData['mark'] = $msg;
                $userVerifyModel->where('id', $id)->update($dbData);
                $this->success("提交成功");
            } else {
                $this->success("提交失败");
            }
        }


    }


    public function verifyDetail()
    {
        $user = $this->auth->getUser();
        $userVerifyModel = new \app\admin\model\Userverify();
        $verify = $userVerifyModel->where('uid', $user['id'])->find();
        $this->success("查询成功", $verify);

    }


}
